package com.sxx.beautifulcity.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.sxx.beautifulcity.R;
import com.sxx.beautifulcity.activity.MainActivity;
import com.sxx.beautifulcity.activity.RecommendSoftActivity;
import com.sxx.beautifulcity.application.ProjectApplication;
import com.sxx.beautifulcity.entity.RecommendSoftData;
import com.sxx.beautifulcity.listenerInterface.OnDownLoadClickListener;

import java.util.List;

/**
 * Created by dllo on 15/11/10.
 */
public class RecommendSoftAdapter extends RecyclerView.Adapter<RecommendSoftAdapter.RecommendSoftAdapterViweHolder> {
    private List<RecommendSoftData> list;
    private OnDownLoadClickListener listener;


    public void setOnDownLoadClickListener(OnDownLoadClickListener listener) {
        this.listener = listener;
    }




    public void addData(List<RecommendSoftData> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    class RecommendSoftAdapterViweHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private SimpleDraweeView softIv;
        private TextView soft_download_times, softTv;
        private Button button;
        private ProgressBar progressBar;


        public RecommendSoftAdapterViweHolder(View itemView) {
            super(itemView);
            softIv = (SimpleDraweeView) itemView.findViewById(R.id.softIv);
            soft_download_times = (TextView) itemView.findViewById(R.id.soft_download_times);
            softTv = (TextView) itemView.findViewById(R.id.softTv);
            button = (Button) itemView.findViewById(R.id.button);
            button.setOnClickListener(this);
            progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar);
            progressBar.setMax(100);

        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.button:
                    if (list != null && list.size() > 0) {
                        if (listener != null) {
                            listener.onDownloadClick(progressBar, list.get(getLayoutPosition()).getDown_url());
                        }
                    }
                    break;


            }
        }
    }

    @Override
    public RecommendSoftAdapterViweHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.activity_recommend_soft, null);

        return new RecommendSoftAdapterViweHolder(view);
    }

    @Override
    public void onBindViewHolder(RecommendSoftAdapterViweHolder holder, int position) {
        if (list != null && list.size() > 0) {
            RecommendSoftData data = list.get(position);
            holder.softTv.setText(data.getName());
            long a = (Long.parseLong(data.getDownload_times()) / 1000000);

            holder.soft_download_times.setText(a + "百万次");
            String path = data.getLogo_url();
            frescoGetImage(path, holder.softIv);
        }
    }

    public void frescoGetImage(String path, SimpleDraweeView draweeView) {
        Uri uri = Uri.parse(path);
        draweeView.setImageURI(uri);
    }

    @Override
    public int getItemCount() {
        return list != null && list.size() > 0 ? list.size() : 0;
    }




}
